package com.swak.frame.excel.validation;


import com.swak.frame.exception.ExcelException;
import com.swak.frame.validation.ValidationResult;

import java.util.List;
import java.util.Map;

public interface BizRowDataValidator<T> {

    ValidationResult validate(List<T> dataList);

    default boolean validate(List<T> dataList, Map<String, Integer> cellMap) {
        ValidationResult validResult = validate(dataList);
        int rowIndex = validResult.getRowIndex();
        if (!validResult.isSuccess()) {
            ValidationResult.ErrorMessage errorMessage = validResult.getErrorMessages().get(0);
            Integer columnIndex = cellMap.get(errorMessage.getPropertyName());
            if (columnIndex != null) {
                throw new ExcelException(String.format("[%s] 第%s行第%s列的值%s",
                        new Object[] { errorMessage.getPropertyName(), (rowIndex + 1), (columnIndex+1),errorMessage.getMessage()}));
            } else {
                throw new ExcelException("["+errorMessage.getPropertyName()+"] 第" + (rowIndex + 1) + "行数据" + errorMessage.getMessage());
            }
        }
        return true;
    }
}
